import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { viteMockServe } from 'vite-plugin-mock'
import { createHtmlPlugin } from 'vite-plugin-html'
import path from 'path'
import dayjs from 'dayjs'
//@ts-ignore
import { px2remOptions } from './src/utils/pxtorem'
//@ts-ignore
import px2rem from 'postcss-plugin-px2rem'
//@ts-ignore
// import common_config from './public/aircity/config.js'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
	const env = loadEnv(mode, process.cwd())
	const { VITE_API_URL, VITE_API_BASE_URL, VITE_FLV_PROXY_URL, VITE_FLV_URL, VITE_ADMIN_PROXY_URL, VITE_ADMIN_URL } = env
	return {
		plugins: [
			vue(),
			// createHtmlPlugin({
			// 	inject: {
			// 		data: {
			// 			title: common_config.title,
			// 		},
			// 	},
			// }),
			AutoImport({
				resolvers: [ElementPlusResolver()],
			}),
			Components({
				dirs: ['src/components/common'],
				resolvers: [ElementPlusResolver()],
			}),
			//mock
			viteMockServe({
				mockPath: './src/mock',
				enable: true,
				logger: true,
			}),
		],
		resolve: {
			alias: {
				'@': path.resolve(__dirname, './src'), // 路径别名
				'@img': path.resolve(__dirname, './src/assets/images'), // 路径别名
			},
			extensions: ['.js', '.ts', '.json'], // 导入时想要省略的扩展名列表
		},
		css: {
			preprocessorOptions: {
				scss: {
					// additionalData: `@import '/src/assets/styles/common.scss';`, // 全局变量
					additionalData: `
						@use "element-plus/theme-chalk/src/index.scss" as *;
						@use "/src/assets/styles/common.scss" as *; // 覆盖变量在此文件
						`,
				},
			},
			postcss: {
				plugins: [px2rem(px2remOptions)],
			},
		},
		server: {
			host: '0.0.0.0',
			port: 3100,
			open: false,
			cors: true,
			proxy: {
				[VITE_API_BASE_URL]: {
					target: VITE_API_URL,
					changeOrigin: true,
					rewrite: (path) => path.replace(new RegExp(VITE_API_BASE_URL), ''), // 不可以省略rewrite
				},
				[VITE_FLV_URL]: {
					target: VITE_FLV_PROXY_URL,
					changeOrigin: true,
					rewrite: (path) => path.replace(new RegExp(VITE_FLV_URL), ''), // 不可以省略rewrite
				},
				[VITE_ADMIN_URL]: {
					target: VITE_ADMIN_PROXY_URL,
					changeOrigin: true,
					rewrite: (path) => path.replace(new RegExp(VITE_ADMIN_URL), ''), // 不可以省略rewrite
				},
				// "/auth_apitoken": {
				//     target: "http://10.99.83.12:8070",
				//     changeOrigin: true,
				//     rewrite(path) {
				//       return path.replace(/^\/auth_apitoken/, "");
				//     }
			},
		},
		build: {
			outDir: `dist`,
			target: 'modules', // 浏览器兼容目标
			assetsDir: 'static', // 静态资源存放路径
			cssCodeSplit: true, // 允许 css 代码拆分
			sourcemap: false, // 生成 sourceMap 文件
			minify: 'terser', // 缩小文件体积
			// terserOptions: {
			// 	compress: {
			// 		drop_console: true, // 取消 console
			// 		drop_debugger: true, // 取消 debugger
			// 		pure_funcs: ['console.*'], //删除所有的console.开头的打印信息
			// 	},
			// 	format: {
			// 		comments: false,
			// 	},
			// },
			// brotliSize: false, //启用/禁用 brotli 压缩大小报告
			chunkSizeWarningLimit: 1800, //chunk 大小警告的限制
			rollupOptions: {
				output: {
					chunkFileNames: 'assets/js/[name]-[hash].js',
					entryFileNames: 'assets/js/[name]-[hash].js',
					assetFileNames: (e) => {
						return ['otf', 'ttf'].includes(e.name!.slice(-3))
							? 'assets/font/[name]-[hash].[ext]'
							: 'assets/[ext]/[name]-[hash].[ext]'
					},
				},
			},
		},
	}
})
